package com.htgd.gardenexpo.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConTouristTraffic;
import com.htgd.gardenexpo.page.TouristTrafficQuery;
import com.htgd.gardenexpo.service.GeConTouristTrafficService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author ：hejing
 * @date ：Created in 2021/5/24 17:14
 * @description：交通服务管理
 * @menu: 交通服务管理
 * @modified By：`
 * @version: 1.0
 */
@Api(tags = "交通服务管理")
@RestController
@RequestMapping("/api/touristTraffic")
public class GeConTouristTrafficServiceController {

    @Autowired
    private GeConTouristTrafficService geConTouristTrafficService;

    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public CommonResult<JSONObject> add(@RequestBody GeConTouristTraffic geConLeisureSports){
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConTouristTrafficService.save(geConLeisureSports);
            result.setCode(200);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @PostMapping(value = "/edit")
    @ApiOperation("编辑")
    public CommonResult<JSONObject> edit(@RequestBody GeConTouristTraffic geConLeisureSports) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            geConTouristTrafficService.updateById(geConLeisureSports);
            result.setCode(200);
            result.setMessage("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
            result.setMessage("修改失败");
        }
        return result;
    }

    @DeleteMapping(value = "/delete")
    @ApiOperation("删除")
    public CommonResult delete(Integer id) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            GeConTouristTraffic geConLeisureSports = new GeConTouristTraffic();
            geConLeisureSports.setId(id);
            geConLeisureSports.setIsDelete(1);
            geConTouristTrafficService.updateById(geConLeisureSports);
            result.setCode(200);
            result.setMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

    @GetMapping(value = "/list")
    @ApiOperation("查询")
    public CommonResult<List<GeConTouristTraffic>> list(TouristTrafficQuery touristTrafficQuery) {
        PageInfo<GeConTouristTraffic> list = geConTouristTrafficService.pageList(touristTrafficQuery);
        return CommonResult.success(String.valueOf(list.getTotal()),list.getList());
    }

    @PostMapping(value = "/review")
    @ApiOperation("审核")
    public CommonResult<JSONObject> review(@RequestBody GeConTouristTraffic geConLeisureSports) {
        CommonResult<JSONObject> result = new CommonResult<>();
        try {
            if(Objects.equals(geConLeisureSports.getStatus(),"3")){
                geConLeisureSports.setReleaseTime(LocalDateTime.now());
            }else{
                geConLeisureSports.setAuthTime(LocalDateTime.now());
            }
            geConTouristTrafficService.updateById(geConLeisureSports);
            result.setCode(200);
            result.setMessage("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
            result.setMessage("操作失败");
        }
        return result;
    }

    @GetMapping(value = "/details")
    @ApiOperation("详情")
    public CommonResult<GeConTouristTraffic> details(Integer id) {
        CommonResult<GeConTouristTraffic> result = new CommonResult<>();
        try {
            GeConTouristTraffic geConTouristTraffic = geConTouristTrafficService.getById(id);
            result.setCode(200);
            result.setMessage("查询成功");
            result.setData(geConTouristTraffic);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(500);
        }
        return result;
    }

}
